package com.smbms.config;

import com.alibaba.druid.pool.DruidDataSource;
import lombok.Data;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;

import javax.sql.DataSource;

@Configuration
@ComponentScan("com.smbms.service")
@MapperScan("com.smbms.mapper")
public class MyBatisConfig {
    //产生数据源
    //产生数据源，存放在容器中使用注解@Bean
    @Bean
    DataSource dataSource(){
        DruidDataSource dataSource=new DruidDataSource();
        dataSource.setDriverClassName("com.mysql.jdbc.Driver");
        dataSource.setUrl("jdbc:mysql:///smbms");
        dataSource.setUsername("root");
        dataSource.setPassword("1234");
        return dataSource;
    }

    //产生会话工厂，存放在容器中
    @Bean
    SqlSessionFactoryBean getSqlSessionFactory(DataSource dataSource){
        SqlSessionFactoryBean ssf=new SqlSessionFactoryBean();
        ssf.setDataSource(dataSource);
        return ssf;
    }
}
